package com.shujia.scala

object Demo9ScalaExtends {
  def main(args: Array[String]): Unit = {
    val a = new A("0001", "张三")
    val b = new B("0001", "张三", 19)
    val c = new C("0001", "张三", "文科一班")

    // 多态：父类引用指向子类对象
    runTwice(a)
    runTwice(b)
    runTwice(c)

    val str: String = "abc"
    val str1: Object = "abc" // 多态

  }

  def runTwice(any: A): Unit = {
    any.run()
    any.run()
  }

}

// 父类
class A(id: String, name: String) {
  val _id = id
  val _name = name
  println("这是A的构造方法")

  def run(): Unit = {
    println("A can run")
  }
}

// 继承的时候使用extends关键字
// 同Java一样只能继承一个父类
// 在extends时就需要调用父类的构造方法
class B(id: String, name: String, age: Int) extends A(id, name) {
  println("这是B的构造方法")

  override def run(): Unit = {
    println("B can run")
  }
}

class C(id: String, name: String, clazz: String) extends A(id, name) {
  println("这是C的构造方法")

  override def run(): Unit = {
    println("C can run")
  }
}